package com.erli.exchange.common.context;

import com.erli.exchange.core.util.RequestUtil;
import com.erli.exchange.core.util.SpringContextHolder;

/**
 * 
 * @Package com.erli.exchange.common.context   
 * @ClassName:  ApiRequestContextFactory   
 * @Description:TODO
 * @Author: LeeHo
 * @Date:   2019年3月16日 下午10:34:02   
 *
 */
public class ApiRequestContextFactory {

	private static final CookieManager cookieManager = SpringContextHolder.getBean(CookieManager.class);
	
	/**
	 * 
	 * @Title: createApiRequestContext   
	 * @Description: 创建ApiRequestContext
	 * @Author: LeeHo
	 * @Date:   2019年3月18日 下午11:37:31      
	 * @param: @param token
	 * @param: @param userId
	 * @param: @return      
	 * @return: ApiRequestContext  
	 * @throws
	 */
	public static ApiRequestContext createApiRequestContext(String token, Integer userId) {
		ApiRequestContext context = ApiRequestContext.builder().userId(userId)
				.loginToken(token).clientIp(RequestUtil.getClientIp())
				.requestUri(RequestUtil.getRequestUri()).build();
		ApiRequestContext.setThreadLocal(context);
		return context;
	}

	/**
	 * 
	 * @Title: createApiRequestContext   
	 * @Description: 从Cookie中拿取信息创建ApiRequestContext
	 * @Author: LeeHo
	 * @Date:   2019年3月16日 下午11:29:18      
	 * @param: @return      
	 * @return: ApiRequestContext  
	 * @throws
	 */
	public static ApiRequestContext createApiRequestContextFromCookie() {
		ApiRequestContext context = ApiRequestContext.builder().userId(cookieManager.getUserId())
				.loginToken(cookieManager.getLoginToken()).clientIp(RequestUtil.getClientIp())
				.requestUri(RequestUtil.getRequestUri()).build();
		ApiRequestContext.setThreadLocal(context);
		return context;
	}

	/**
	 * 
	 * @Title: getApiRequestContext   
	 * @Description: 获取ApiRequestContext
	 * @Author: LeeHo
	 * @Date:   2019年3月16日 下午11:29:29      
	 * @param: @return      
	 * @return: ApiRequestContext  
	 * @throws
	 */
	public static ApiRequestContext getApiRequestContext() {
		return ApiRequestContext.getThreadLocal();
	}

}
